package br.com.skytef.beans.WEB;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import br.com.skytef.dao.DaoPos;
import br.com.skytef.dao.EstabelecimentoDao;
import br.com.skytef.entity.EstabelecimentoEntity;
import br.com.skytef.entity.PosEntity;

@ManagedBean(name = "CadPos")
@RequestScoped
@ViewScoped
public class CadPosBean {
	private List<EstabelecimentoEntity> estabelecimentos;
	private int idEstab = 0;
	private String posSerial = "";
	private String posSerialFind = "";

	@PostConstruct
	public void ini() {
		this.load();
	}

	public void load() {
		try {
			EstabelecimentoDao daoEst = new EstabelecimentoDao();
			estabelecimentos = daoEst.buscarTodos();
		} catch (Exception e) {
			String msg = "Erro : \n" + e.getMessage();
			this.message(FacesMessage.SEVERITY_ERROR, "Erro", msg);
		}
	}

	/*----------------------------------------------------------------------------------
	 * Acoes
	 *---------------------------------------------------------------------------------*/

	public void save() {
		try {
			PosEntity posSav = new PosEntity();
			PosEntity pos = new PosEntity();
			pos.setIdCompany(idEstab);
			pos.setSerial(posSerial);

			DaoPos dao = new DaoPos(true);
			posSav = dao.selectPosBySerial(posSerial);

			if (posSav == null) {
				dao.insertPos(pos);
				this.message(FacesMessage.SEVERITY_INFO, "Inserido","Registro salvo com sucesso.");
			} else{
				dao.updatePos(pos);
				this.message(FacesMessage.SEVERITY_INFO, "Atualizado","Registro salvo com sucesso.");
			}
			this.refresh();
		} catch (Exception e) {
			String msg = "Erro ao atualizar o registro: \n" + e.getMessage();
			this.message(FacesMessage.SEVERITY_ERROR, "Erro", msg);
		}
	}

	public void delete() {
		try {
			DaoPos dao = new DaoPos(true);
			dao.deletePos(posSerial);
			this.message(FacesMessage.SEVERITY_INFO, "Excluido","Registro excluido com sucesso.");
			this.refresh();
		} catch (Exception e) {
			String msg = "Erro ao excluir o registro: \n" + e.getMessage();
			this.message(FacesMessage.SEVERITY_ERROR, "Erro", msg);
		}
	}

	public void find() {
		try {
			PosEntity pos = new PosEntity();

			DaoPos dao = new DaoPos(true);
			pos = dao.selectPosBySerial(posSerialFind);

			idEstab = pos.getIdCompany();
			posSerial = pos.getSerial();
			this.load();
		} catch (Exception e) {
			String msg = "Erro ao localizar o registro: \n" + e.getMessage();
			this.message(FacesMessage.SEVERITY_ERROR, "Erro", msg);
		}
	}

	public void refresh() {
		try {
			idEstab = 0;
			posSerial = "";
			posSerialFind = "";
			this.load();
		} catch (Exception e) {
			String msg = "Erro : \n" + e.getMessage();
			this.message(FacesMessage.SEVERITY_ERROR, "Erro", msg);
		}
	}

	/*----------------------------------------------------------------------------------
	 * mensagens
	 *---------------------------------------------------------------------------------*/
	public void message(FacesMessage.Severity type, String Title, String message) {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		facesContext.addMessage(null, new FacesMessage(type, Title, message));
	}

	/*----------------------------------------------------------------------------------
	 * Set get
	 *---------------------------------------------------------------------------------*/
	public List<EstabelecimentoEntity> getEstabelecimentos() {
		return estabelecimentos;
	}

	public void setEstabelecimentos(List<EstabelecimentoEntity> estabelecimentos) {
		this.estabelecimentos = estabelecimentos;
	}

	public int getIdEstab() {
		return idEstab;
	}

	public void setIdEstab(int idEstab) {
		this.idEstab = idEstab;
	}

	public String getPosSerial() {
		return posSerial;
	}

	public void setPosSerial(String posSerial) {
		this.posSerial = posSerial;
	}

	public String getPosSerialFind() {
		return posSerialFind;
	}

	public void setPosSerialFind(String posSerialFind) {
		this.posSerialFind = posSerialFind;
	}

}
